
#include "AdventureMgr.h"
#include "LogicService.h"
#include "Character/CreatureMgr.h"
#include "Character/monster/Monster.h"
#include "Common/TableData/ActivityDailyCfg.h"
#include "Character/AdventurePart.h"
#include "Common/TableData/ConstantConstantCfg.h"
#include "Common/TableData/AreaAreaCfg.h"
#include "Common/TableData/NpcNpcCfg.h"
#include "Common/TableData/DuplicateDuplicateCfg.h"
#include "Common/TableData/ItemItemCfg.h"
#include "Common/CampDefine.h"
#include "Common/DeputyDefine.h"
#include "Common/TableData/MonsterMonsterCfg.h"
#include "Mission/MissionManager.h"
#include "Common/TableData/AvocationComposeCfg.h"

AdventureMgr::AdventureMgr()
{
	m_mapAdventureCfg.clear();
	m_adventureAcceptTime = 0;
	m_mapActionAdventureMap.clear();
}

AdventureMgr::~AdventureMgr()
{
}

bool AdventureMgr::Init()
{
	const ConstantConstantCfgInfo *pConstCfg = g_GetConstantConstantCfgTable()->GetConstantConstantCfgInfo(EArg_AdventureAcceptTime);
	if (nullptr == pConstCfg)
	{
		LogErrFmtPrint("AdventureMgr::Init...nulpltr == pConstCfg...id:%d ", EArg_AdventureAcceptTime);
		return false;
	}
	//
	m_adventureAcceptTime = pConstCfg->constantdata;

	if (!LoadConfig("./data/csv/encounter.csv"))
	{
		return false;
	}

	return true;
}

void AdventureMgr::UnInit()
{
	g_GetEvent()->UnSubscribeAll(this);
	m_mapAdventureCfg.clear();
	m_adventureAcceptTime = 0;
	m_mapActionAdventureMap.clear();	
}

void AdventureMgr::OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen)
{
}

//加载配置
bool AdventureMgr::LoadConfig(const char *szFileName)
{
	CsvParser reader(3);
	if (reader.LoadFromFile(szFileName))
	{
		MMOLOG_FMT_ERROR("[logic] AdventureMgr::LoadConfig  reader.LoadFromFile is error");
		return false;
	}
	int32_t row = reader.GetHeight();
	int32_t ret = 0;
	char str[1024] = { 0 };
	for (int32_t i = 4; i <= row; ++i)
	{
		AdventureCfgInfo info;
		reader.GetInteger(i, "id", 0, &ret);
		info.adventureId = ret;
		ret = 0;

		reader.GetInteger(i, "chapter", 0, &ret);
		info.chapterId = ret;
		ret = 0;

		//校验章节ID
		const MissionChapterInfo *pChapterCfg = g_GetMissionManager()->GetMissionChapterCfgInfo(info.chapterId);
		if (nullptr == pChapterCfg)
		{
			MMOLOG_FMT_ERROR("[logic] AdventureMgr::LoadConfig  nullptr == pChapterCfg...adventureid:%u,chapterid:%u ",info.adventureId,info.chapterId);
			return false;
		}
		if (pChapterCfg->kind != MISSION_TYPE_ID_ADVENTURE)
		{
			MMOLOG_FMT_ERROR("[logic] AdventureMgr::LoadConfig  chapter kind error...adventureid:%u,chapterid:%u,kind:%u ", info.adventureId, info.chapterId,pChapterCfg->kind);
			return false;
		}

		reader.GetString(i, "action", "", str, 1024);
		string strParam = str;
		SActionParam param;
		param.adventureId = info.adventureId;
		if (!ParseAction(info.action, strParam, param))
		{
			return false;
		}
		memset(str, '\0', sizeof(str));


		reader.GetString(i, "condition", "", str, 1024);
		string strParamA = str;
		SParaseAcceptParam paramA;
		paramA.adventureId = info.adventureId;
		if (!g_GetMissionManager()->ParseAcceptCond(info.accept, strParamA, paramA))
		{
			return false;
		}
		memset(str, '\0', sizeof(str));
		
		AdventureCfgMap::iterator iter = m_mapAdventureCfg.find(info.adventureId);
		if (iter != m_mapAdventureCfg.end())
		{
			MMOLOG_FMT_ERROR("AdventureMgr::LoadConfig...repeated adventure data...adventureid:%d ", info.adventureId);
			return false;
		}
		m_mapAdventureCfg[info.adventureId] = info;

		UpdateActionAdventure(info.action.actionType,info.adventureId);
	}

	return true;
}

//解析行为
bool AdventureMgr::ParseAction(ActionCfgInfo &action, string strParam, SActionParam &param)
{
	VEC_STRING vecStr;
	vecStr.clear();
	//接取分隔符
	CommonApi::SplitStr(strParam, "=", &vecStr);
	int32_t isize = vecStr.size();
	if (isize != 6)
	{
		MMOLOG_FMT_ERROR("AdventureMgr::ParseAction....isize != 6...adventureid:%u, isize:%d ", param.adventureId, isize);
		return false;
	}
	//第一位表示的是类型
	if (vecStr[0] == "")
	{
		MMOLOG_FMT_ERROR("AdventureMgr::ParseAction....vecStr[0] == ""...adventureid:%u, str:%s ",  param.adventureId, strParam.c_str());
		return false;
		return false;
	}
	int32_t actionType = atoi(vecStr[0].c_str());
	int32_t actionVal = atoi(vecStr[1].c_str());
	int32_t param1 = atoi(vecStr[2].c_str());
	int32_t param2 = atoi(vecStr[3].c_str());
	if (EAdventureAction_EnterArea == actionType
		|| EAdventureAction_LeaveArea == actionType
		)
	{
		const AreaAreaCfgInfo *pAreaCfg = g_GetAreaAreaCfgTable()->GetAreaAreaCfgInfo(actionVal);
		if (nullptr == pAreaCfg)
		{
			MMOLOG_FMT_ERROR("AdventureMgr::ParseAction...nullptr == pAreaCfg....adventureid:%u,type:%d,areaid:%d ", param.adventureId, actionType, actionVal);
			return false;
		}
	}
	else if (EAdventureAction_NpcTalk == actionType)
	{
		const NpcNpcCfgInfo *pNpcCfg = g_GetNpcNpcCfgTable()->GetNpcNpcCfgInfo(actionVal);
		if (nullptr == pNpcCfg)
		{
			MMOLOG_FMT_ERROR("AdventureMgr::ParseAction...nullptr == pNpcCfg....adventureid:%u,type:%d,npcid:%d ", param.adventureId, actionType, actionVal);
			return false;
		}
	}
	else if (EAdventureAction_EnterDup == actionType)
	{
		const DuplicateDuplicateCfgInfo *pDupCfg = g_GetDuplicateDuplicateCfgTable()->GetDuplicateDuplicateCfgInfo(actionVal);
		if (nullptr == pDupCfg)
		{
			MMOLOG_FMT_ERROR("AdventureMgr::ParseAction...nullptr == pDupCfg....adventureid:%u,type:%d,dupid:%d ", param.adventureId, actionType, actionVal);
			return false;
		}
	}
	else if (EAdventureAction_ItemUse == actionType
		|| EAdventureAction_ItemAdd == actionType
		)
	{
		const ItemItemCfgInfo *pItemCfg = g_GetItemItemCfgTable()->GetItemItemCfgInfo(actionVal);
		if (nullptr == pItemCfg)
		{
			MMOLOG_FMT_ERROR("AdventureMgr::ParseAction...nullptr == pItemCfg....adventureid:%u,type:%d,itemid:%d ", param.adventureId, actionType, actionVal);
			return false;
		}
	}
	else if (EAdventureAction_CampChange == actionType)
	{
		if (actionVal != ECamp_Type_Dark && actionVal != ECamp_Type_Light && actionVal != ECamp_Type_None)
		{
			MMOLOG_FMT_ERROR("AdventureMgr::ParseAction...campid error....adventureid:%u,type:%d,campid:%d ", param.adventureId, actionType, actionVal);
			return false;
		}
	}
	else if (EAdventureAction_Collect == actionType)
	{
		if (actionVal <= ECollectDeputyType_None || actionVal >= ECollectDeputyType_Limit)
		{
			MMOLOG_FMT_ERROR("AdventureMgr::ParseAction...actionVal <= ECollectDeputyType_None || actionVal >= ECollectDeputyType_Limit....adventureid:%u,type:%d,actionVal:%d ", param.adventureId, actionType, actionVal);
			return false;
		}
	}
	else if (EAdventureAction_Make == actionType)
	{
		if (actionVal <= EMakeDeputyType_None || actionVal >= EMakeDeputyType_Limit)
		{
			MMOLOG_FMT_ERROR("AdventureMgr::ParseAction...actionVal <= EMakeDeputyType_None || actionVal >= EMakeDeputyType_Limit....adventureid:%u,type:%d,actionVal:%d ", param.adventureId, actionType, actionVal);
			return false;
		}
	}
	else if (EAdventureAction_KillMons == actionType 
		|| EAdventureAction_MonsCount == actionType
		)
	{
		const MonsterMonsterCfgInfo *pMonsCfg = g_GetMonsterMonsterCfgTable()->GetMonsterMonsterCfgInfo(actionVal);
		if (nullptr == pMonsCfg)
		{
			MMOLOG_FMT_ERROR("AdventureMgr::ParseAction...nullptr == pMonsCfg....adventureid:%u,type:%d,monsid:%d ", param.adventureId, actionType, actionVal);
			return false;
		}
	}

	action.actionType = actionType;
	action.actionVal = actionVal;
	action.param1 = param1;
	action.param2 = param2;

	return true;
}

//更新奇遇行为和奇遇ID列表
void AdventureMgr::UpdateActionAdventure(int32_t actionType, uint32_t adventureId)
{
	MAP_INT32_SET_UINT32::iterator iter = m_mapActionAdventureMap.find(actionType);
	if (iter != m_mapActionAdventureMap.end())
	{
		iter->second.insert(adventureId);
	}
	else
	{
		SET_UINT32 setAdventure;
		setAdventure.clear();
		setAdventure.insert(adventureId);
		m_mapActionAdventureMap[actionType] = setAdventure;
	}
}

//根据奇遇行为获取奇遇ID列表
const SET_UINT32 *AdventureMgr::GetAdventureByAction(int32_t actionType)
{
	MAP_INT32_SET_UINT32::iterator iter = m_mapActionAdventureMap.find(actionType);
	return (iter != m_mapActionAdventureMap.end()) ? &iter->second : nullptr;
}

//是否是奇遇行为
bool AdventureMgr::IsAdventureAction(int32_t actionType)
{
	MAP_INT32_SET_UINT32::iterator iter = m_mapActionAdventureMap.find(actionType);
	return (iter != m_mapActionAdventureMap.end()) ? true : false;
}

//奇遇任务接取倒计时
int32_t AdventureMgr::AdventureAcceptTime()
{
	return m_adventureAcceptTime;
}

//获取奇遇配置信息
const AdventureCfgInfo *AdventureMgr::GetAdventureCfgInfo(uint32_t adventureId)
{
	AdventureCfgMap::iterator iter = m_mapAdventureCfg.find(adventureId);
	return (iter != m_mapAdventureCfg.end()) ? &iter->second : nullptr;
}
